/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.CatalogBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.ServiceCatalog;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class CatalogDetailSubContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String catlogId = null;
	private GwtContext context;

	/**
	 * keep this url in case of modify
	 */
	private String url = null;

	private VerticalPanel p = null;

	private ServiceCatalog sc = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList ret = (ArrayList) res.getResponse();
			sc = (ServiceCatalog) ret.get(0);
			VerticalPanel v = new VerticalPanel();
			v.setWidth("100%");
			v.setBorderWidth(0);

			HTML hh = new HTML(cons.catalogBasic());
			v.add(hh);
			Grid g = new Grid(5, 2);
			g.setWidth("100%");
			g.setCellPadding(1);
			g.setCellSpacing(1);
			g.setBorderWidth(1);

			ColumnFormatter ccf = g.getColumnFormatter();
			ccf.setWidth(0, "15%");
			ccf.setWidth(1, "50%");

			CellFormatter cff = g.getCellFormatter();
			cff.setStyleName(0, 0, "tablecol");
			cff.setStyleName(1, 0, "tablecol");
			cff.setStyleName(2, 0, "tablecol");
			cff.setStyleName(3, 0, "tablecol");
			cff.setStyleName(4, 0, "tablecol");

			g.setHTML(0, 0, cons.name());
			g.setHTML(1, 0, cons.code());
			g.setHTML(2, 0, cons.appServer());
			g.setHTML(3, 0, cons.serVersion());

			g.setHTML(4, 0, cons.desc());

			g.setHTML(0, 1, sc.getName());
			g.setHTML(1, 1, sc.getCode());
			g.setHTML(2, 1, sc.getAppServer());
			g.setHTML(3, 1, sc.getServerVersion());

			g.setHTML(4, 1, sc.getDescription());
			v.add(g);

			HorizontalPanel h1 = new HorizontalPanel();
			Button b = new Button(cons.modify());
			b.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap m = new HashMap();
					m.put("cat", sc);
					m.put("url", url);
					listener.onContentChanged(
							DomainContentConstants.CAT_DETAIL,
							DomainContentConstants.MODIFY_CAT1, m);

				}
			});

			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					HashMap m = new HashMap();
					m.put("cat", sc);
					listener.onContentChanged(
							DomainContentConstants.CAT_DETAIL,
							DomainContentConstants.REMOVE_CAT, m);

				}
			});
			h1.add(b);
			h1.add(b1);
			if (PrivilegChecker.hasPrivilege(context, null))
				v.add(h1);

			p.add(v);

			VerticalPanel v1 = new VerticalPanel();
			v1.setWidth("80%");
			v1.setSpacing(10);

			hh = new HTML(cons.catalogBinding());
			v1.add(hh);

			ArrayList bindings = (ArrayList) ret.get(1);
			for (int i = 0, j = bindings.size(); i < j; i++) {
				CatalogBinding cb = (CatalogBinding) bindings.get(i);
				if (cb.getType().equals(EntityConstants.BINDING_HTTP)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("100%");
					p1.setBorderWidth(0);
					HttpBinding hb = (HttpBinding) cb.getBinding();
					HTML h = new HTML();

					if (cb.getName().equals(EntityConstants.CAT_BINDING_QUERY)) {
						h.setHTML(cons.queryBinding());
						url = StringUtils.getIPPORTOfQueryUrl(hb.getUrl());
					} else if (cb.getName().equals(
							EntityConstants.CAT_BINDING_MONITOR)) {
						h.setHTML(cons.monitorBinding());

					} else if (cb.getName().equals(
							EntityConstants.CAT_BINDING_SECURITY)) {
						h.setHTML(cons.securityBinding());

					}
					p1.add(h);
					Grid table = new Grid(3, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.url());

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.http());
					table.setHTML(2, 1, hb.getUrl());

					p1.add(table);
					v1.add(p1);
				} else if (cb.getType().equals(EntityConstants.BINDING_JMS)) {
					VerticalPanel p1 = new VerticalPanel();
					p1.setWidth("100%");
					p1.setBorderWidth(0);

					JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
					JmsBinding jb = w.getJmsBinding();
					NormalJms info = (NormalJms) w.getInfo();

					Grid table = new Grid(10, 2);
					table.setWidth("100%");
					ColumnFormatter cf = table.getColumnFormatter();
					cf.setWidth(0, "20%");
					cf.setWidth(1, "50%");

					table.setCellPadding(1);
					table.setCellSpacing(1);
					table.setBorderWidth(1);

					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(0, 0, "tablecol");
					ff.setStyleName(1, 0, "tablecol");
					ff.setStyleName(2, 0, "tablecol");
					ff.setStyleName(3, 0, "tablecol");
					ff.setStyleName(4, 0, "tablecol");
					ff.setStyleName(5, 0, "tablecol");
					ff.setStyleName(6, 0, "tablecol");
					ff.setStyleName(7, 0, "tablecol");
					ff.setStyleName(8, 0, "tablecol");
					ff.setStyleName(9, 0, "tablecol");

					table.setHTML(0, 0, cons.name());
					table.setHTML(1, 0, cons.type());
					table.setHTML(2, 0, cons.jmsServer());
					table.setHTML(3, 0, cons.jmsServerVersion());
					table.setHTML(4, 0, cons.jmsServerUrl());
					table.setHTML(5, 0, cons.jmsFac());
					table.setHTML(6, 0, cons.jmsDestination());
					table.setHTML(7, 0, cons.jmsType());
					table.setHTML(8, 0, cons.jmsUser());
					table.setHTML(9, 0, cons.jmsPass());

					HTML h = new HTML(cons.statBinding());
					p1.add(h);

					table.setWidget(0, 1, h);
					table.setHTML(1, 1, cons.jms());

					table.setHTML(2, 1, info.getServerUrl());
					table.setHTML(3, 1, jb.getServer());
					table.setHTML(4, 1, jb.getServerVersion());

					table.setHTML(5, 1, info.getFactoryUrl());
					table.setHTML(6, 1, info.getDestinationJndi());
					table.setHTML(7, 1, cons.jmsTypeQueue());
					// jmsUser = new TextBox();
					// jmsUser.setVisibleLength(32);
					// ;
					// jmsUser.setText(jb.getUser());
					table.setHTML(8, 1, "&nbsp;");
					// jmsPass = new PasswordTextBox();
					// jmsPass.setVisibleLength(32);

					// jmsPass.setText(jb.getPassword());
					table.setHTML(9, 1, "&nbsp;");

					p1.add(table);
					v1.add(p1);

				}

			}
			p.add(v1);

		}

	};

	public CatalogDetailSubContent(DomainMessages cons,
			BifangGWTServiceAsync service, String catId, GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.catlogId = catId;
		this.context = context;

		p = new VerticalPanel();
		p.setSpacing(25);
		p.setWidth("100%");
		initWidget(p);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req.setRequest(this.catlogId);
		req
				.setAction("han.bifang.gwt.server.action.domain.GetCatalogDetailAction");
		service.call(req, cb);

	}

	public void onUnload() {
		this.sc = null;

	}

}
